<template>
  <div>
    ==={{ target }}
    <HomePanel ref="target" title="人气推荐" sub-title="人气爆款 不容错过">
      <template #default>
        <ul ref="pannel" class="goods-list">
          <li v-for="item in list" :key="item.id">
            <RouterLink to="/">
              <img :src="item.picture" alt="" />
              <p class="name">{{ item.title }}</p>
              <p class="desc">&yen;{{ item.alt }}</p>
            </RouterLink>
          </li>
        </ul>
      </template>
    </HomePanel>
  </div>
</template>

<script>
import HomePanel from './home-panel'
import { reqFindHot } from '@/api/home'
import { ref } from 'vue'
import { useIntersectionObserver } from '@vueuse/core'

export default {
  components: {
    HomePanel
  },
  setup () {
    const list = ref([])
    const getList = async () => {
      const res = await reqFindHot()
      console.log(res, 2)
      list.value = res.result
    }

    // 通过 ref 获得组件实例
    const target = ref(null)
    const { stop } = useIntersectionObserver(
      // target 是观察的目标dom容器，必须是dom容器，而且是vue3.0方式绑定的dom对象
      target,
      // isIntersecting 是否进入可视区域，true是进入 false是移出
      // observerElement 被观察的dom
      ([{ isIntersecting }], observerElement) => {
        // 在此处可根据isIntersecting来判断，然后做业务
        console.log(isIntersecting)
        if (isIntersecting) {
          stop()
          getList()
        }
      }
    )
    return { list, target }
  }
}
</script>
<style scoped lang="less">
.goods-list {
  display: flex;
  justify-content: space-between;
  height: 406px;
  li {
    width: 306px;
    height: 406px;
    background: #f0f9f4;
    img {
      width: 306px;
      height: 306px;
    }
    p {
      font-size: 22px;
      padding: 12px 30px 0 30px;
      text-align: center;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }
    .price {
      color: @priceColor;
    }
    .desc {
      color: #999;
      font-size: 18px;
    }
  }
}
</style>
